package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl implements PermissionService {

   //注入DAO对象
    @Autowired
    private PermissionDao permissionDao;

    //添加方法
    @Override
    public void add(Permission permission) {
        permissionDao.add(permission);
    }


    //根据条件分页查询
    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);

        Page<Permission> page =permissionDao.selectByCondition(queryString);

        long total = page.getTotal();
        List<Permission> rows = page.getResult();

        return new PageResult(total,rows);
    }

    //根据id删除检查项
    @Override
    public void delete(Integer id) {
//        System.out.println(id);

//        判断要删除的权限是否和角色关联
        Long count = permissionDao.findCountByPermissionId(id);
        if (count > 0){
            //当前权限已经被关联到角色，不允许删除
            new RuntimeException();
        }
        permissionDao.deleteById(id);

    }

    //根据id查询数据.完成回显
    @Override
    public Permission findById(Integer id) {

        return permissionDao.findById(id);

    }

    @Override
    public void edit(Permission permission) {

        permissionDao.edit(permission);

    }

    @Override
    public Set<Permission> findAll() {
        return permissionDao.findAll();
    }

    //查询所有检查项
    

}
